package com.ccx.coachsite.bus;

import java.io.File;

import com.ccx.coachsite.util.FileUtil;
import com.ccx.coachsite.util.StringUtil;

import android.content.Context;

public class AccountMgr {

	private static AccountMgr mInstance = new AccountMgr();
	public static final String ACCOUNT_PREFERENCE = "accountInfo";

	private AccountMgr() 
	{
	}

	public static AccountMgr getInstance() {
		if (mInstance == null)
			mInstance = new AccountMgr();
		return mInstance;
	}
	/**
	 * 保存账户信息
	 * @param context
	 * @param account
	 */
	public static void saveAccountInfo(final Context context, final Account account) {
		if (context != null && account != null) 
		{
			removeAccountInfo(context);
			FileUtil.write(context, ACCOUNT_PREFERENCE, account);
		}
	}

	/**
	 * 删除用户信息
	 * @param ctx
	 */
	public static void removeAccountInfo(Context ctx) {
		File accountFile = ctx.getFileStreamPath(ACCOUNT_PREFERENCE);
		if (accountFile != null && accountFile.exists()) {
			accountFile.delete();
		}
	}

	/**
	 * 获取用户信息
	 * @param context
	 * @return
	 */
	public static Account readAccountInfo(Context context) {
		Account mAccount = null;
		File accountFile = context.getFileStreamPath(ACCOUNT_PREFERENCE);
		if (accountFile != null && accountFile.exists())
		{
			mAccount = (Account) FileUtil.read(context, ACCOUNT_PREFERENCE);
		}
		 
		if (mAccount == null) {
			return new Account();
		}

		return mAccount;
	}

	/**
	 * 用户是否存在
	 * @param account
	 * @return
	 */
	public static boolean isAccountExist(Account account) {
		if (account == null) {
			return false;
		}
		if (!StringUtil.isEmpty(account.getMobile()) && !StringUtil.isEmpty(account.getImsi())) {
			return true;
		}
		return false;
	}
}
